import Flutter
import UIKit

public class FlutterAutoTrackPlugin: NSObject, FlutterPlugin {
    
    /// 应用启动时的launchOptions
    static var launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "com.xq.auto.track.channel", binaryMessenger: registrar.messenger())
        let instance = FlutterAutoTrackPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getPlatformVersion":
            result("iOS " + UIDevice.current.systemVersion)
        case "auto_track_setup":
            FANativeAutoTrack.setupWith(value: call.arguments)
            result(1)
        case "auto_track_event":
            FANativeAutoTrack.eventWith(arguments: call.arguments)
            result(1)
        case "request_ios_idfa":
            FAIDFARequest.idfaPermission(arguments: call.arguments,
                                         callback: result)
        case "query_device_bd_did":
            FANativeAutoTrack.queryDeviceBDDIDValue(arguments: call.arguments, callback: result)
        case "check_device_is_safe":
            result(FADeviceCheck.currentDeviceIsSafe())
        case "request_ios_click_id":
            result(FANativeAutoTrack.queryClickId())
        default:
            result(FlutterMethodNotImplemented)
        }
    }
    
    /// 提前准备应用启动时的options
    public static func prepareLaunchOptions(_ options: [UIApplication.LaunchOptionsKey: Any]?) {
        launchOptions = options
    }
}
